<?php
/*
Plugin Name: G-Shortcode
Plugin URI: http://googlo.me/archives/3232.html
Description: 乐趣公园短代码插件,本插件只在文本环境下有效！！注意：必须先将文字选择，然后点击按钮才可以。
Version: 1.0
Author: 云落
Author URI: http://googlo.me/
*/
//头部加载CSS样式
function shortcode_head() {
	print('
<link rel="stylesheet" href="'.plugins_url('',__FILE__).'/shortcode.css" type="text/css" />
	');
}
add_action('wp_head', 'shortcode_head');
/*短代码信息框 开始*/
/*绿色提醒框*/
function tozi($atts, $content=null){
    return '<div id="sc_notice">'.$content.'</div>';
}
add_shortcode('iv_notice','tozi');
/*红色提醒框*/
function toai($atts, $content=null){
    return '<div id="sc_error">'.$content.'</div>';
}
add_shortcode('iv_error','toai');
/*黄色提醒框*/
function toci($atts, $content=null){
    return '<div id="sc_warn">'.$content.'</div>';
}
add_shortcode('iv_warn','toci');
/*灰色提醒框*/
function tobi($atts, $content=null){
    return '<div id="sc_tips">'.$content.'</div>';
}
add_shortcode('iv_tips','tobi');
/*蓝色提醒框*/
function todi($atts, $content=null){
    return '<div id="sc_blue">'.$content.'</div>';
}
add_shortcode('iv_blue','todi');
/*蓝边文本框*/
function toei($atts, $content=null){
    return '<div  class="sc_act">'.$content.'</div>';
}
add_shortcode('iv_act','toei');
/*橙色文本框*/
function tofi($atts, $content=null){
    return '<div id="sc_organge">'.$content.'</div>';
}
add_shortcode('iv_organge','tofi');
/*青色文本框*/
function togi($atts, $content=null){
    return '<div id="sc_qing">'.$content.'</div>';
}
add_shortcode('iv_qing','togi');
/*粉色文本框*/
function tohi($atts, $content=null){
    return '<div id="sc_pink">'.$content.'</div>';
}
add_shortcode('iv_pink','tohi');
/*绿色按钮*/
function toii($atts, $content=null) {
   extract(shortcode_atts(array("href" => 'http://'), $atts));
	return '<a class="greenbtn" href="'.$href.'" target="_blank" rel="nofollow">'.$content.'</a>';
}
add_shortcode('igb' , 'toii');
/*蓝色按钮*/
function toji($atts, $content=null) {
   extract(shortcode_atts(array("href" => 'http://'), $atts));
	return '<a class="bluebtn" href="'.$href.'" target="_blank" rel="nofollow">'.$content.'</a>';
}
add_shortcode('ibb' , 'toji');
/*黄色按钮*/
function toki($atts, $content=null) {
   extract(shortcode_atts(array("href" => 'http://'), $atts));
	return '<a class="yellowbtn" href="'.$href.'" target="_blank" rel="nofollow">'.$content.'</a>';
}
add_shortcode('iyb' , 'toki');
/*添加音乐按钮*/
function toli($atts, $content=null){
    return '<audio style="width:100%;max-height:40px;" src="'.$content.'" controls preload loop>您的浏览器不支持HTML5的 audio 标签，无法为您播放！</audio>';
}
add_shortcode('imusic','toli');
//为WordPress添加展开收缩功能
function xcollapsei($atts, $content = null){
	extract(shortcode_atts(array("title"=>""),$atts));
	return '<div style="margin: 0.5em 0;">
		<div class="xControl">
			<span class="xTitle">'.$title.'</span>
			<a href="javascript:void(0)" class="collapseButton xButton">展开/收缩▼</a>
			<div style="clear: both;"></div>
		</div>
		<div class="xContent" style="display: none;">'.$content.'</div>
	</div>';
}
add_shortcode('icollapse', 'xcollapsei');
//使用短代码添加回复后可见内容开始
function reply_to_readi($atts, $content=null){
	extract(shortcode_atts(array("notice" => '<blockquote><p class="reply-to-read" style="color: blue;">注意：本段内容须成功“<a href="'. get_permalink().'#respond" title="回复本文">回复本文</a>”后“<a href="javascript:window.location.reload();" title="刷新本页">刷新本页</a>”方可查看！</p></blockquote>'), $atts));
	if(is_super_admin()){
		$return = '<div class="showhide"><h4>本文隐藏的内容</h4>';
		$return .= $content;
		$return .= '</div>';//如果用户是管理员则直接显示内容
	}
	$email = null;
	$user_ID = (int)wp_get_current_user()->ID;
	if($user_ID > 0){
		$email = get_userdata($user_ID)->user_email;	//如果用户已经登入则从用户信息中取得邮箱
	}else if(isset($_COOKIE['comment_author_email_'.COOKIEHASH])){
		$email = str_replace('%40', '@', $_COOKIE['comment_author_email_'.COOKIEHASH]);		//如果用户尚未登入但COOKIE内储存有邮箱信息
	}else{
		return $notice;		//如无法获取邮箱则返回提示信息
	}
	if(empty($email)){
		return $notice;		//如邮箱为空则返回提示信息
	}
	global $wpdb;
	$post_id = get_the_ID();	//获取文章的ID
	$query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";
	if($wpdb->get_results($query)){
		$return = '<div class="showhide"><h4>本文隐藏的内容</h4>';
		$return .= $content;
		$return .= '</div>';		//查询到对应的评论即正常显示内容
	}else{
		return $notice;			//否则返回提示信息
	}
}
add_shortcode('ireply', 'reply_to_readi');
/* 短代码信息框 完毕*/
//添加编辑器按钮
function shortcode($mce_settings) {
?>
<script type="text/javascript">
QTags.addButton( 'iv_notice', '绿色通知', "[iv_notice]", "[/iv_notice]" );
QTags.addButton( 'iv_error', '红色警告', "[iv_error]", "[/iv_error]" );
QTags.addButton( 'iv_warn', '黄色错误', "[iv_warn]", "[/iv_warn]" );
QTags.addButton( 'iv_tips', '灰色提示', "[iv_tips]", "[/iv_tips]" );
QTags.addButton( 'iv_blue', '蓝色提示', "[iv_blue]", "[/iv_blue]" );
QTags.addButton( 'iv_act', '蓝边文本', "[iv_act]", "[/iv_act]" );
QTags.addButton( 'iv_organge', '橙色文本', "[iv_organge]", "[/iv_organge]" );
QTags.addButton( 'iv_qing', '青色文本', "[iv_qing]", "[/iv_qing]" );
QTags.addButton( 'iv_pink', '粉色文本', "[iv_pink]", "[/iv_pink]" );
QTags.addButton( 'igb', '绿色按钮', "[igb href='']", "[/igb]" );
QTags.addButton( 'ibb', '蓝色按钮', "[ibb href='']", "[/ibb]" );
QTags.addButton( 'iyb', '黄色按钮', "[iyb href='']", "[/iyb]" );
QTags.addButton( 'imusic', '音乐按钮', "[imusic]", "[/imusic]" );
QTags.addButton( 'icollapse', '隐藏收缩', "[icollapse]", "[/icollapse]" );
QTags.addButton( 'ireply', '回复可见', "[ireply]", "[/ireply]" );
QTags.addButton( 'inextpage', '下一页', '<!--nextpage-->', "" );
function shortcode() {
}
</script>
<?php
}
add_action('after_wp_tiny_mce', 'shortcode');
?>